<template>
  <div class="goodsClassify">
    <div class="main">

      <!-- 类目选择器 -->
      <div class="cate-container">

        <div class="c-c-mainWarp clearfix">
          <!-- 分类循环 -->
          <div class="c-c-list fl" v-for="(list, listIndex) in classifyList" :key="listIndex">
            <!-- 搜索分类框 -->
            <!-- <div class="cc-cbox-filter" v-if="listIndex != 0">
              <div class="fl icon">
                <div class="icon-search"></div>
              </div>
              <div class="cc-inpWarp">
                <input type="text" placeholder="输入名称／拼音首字母" @keyup.enter="onSearchClassify">
              </div>
            </div> -->
            <ul>
              <!-- 分类列表元素 -->
              <li class="c-c-item clearfix" 
                v-for="(classify, classifyIndex) in list"
                :key="classifyIndex"
                :class="{'actived': classify.isSelected}" 
                @click="onNextClassify(classify, list, listIndex)">
                <div class="fl">{{classify.className}}</div>
                <div class="fr">&gt;</div>
              </li>
            </ul>
          </div>
          <!-- 二级分类 -->
          <!-- <div class="c-c-list fl">
            <div class="cc-cbox-filter">
              <div class="fl icon">
                <div class="icon-search"></div>
              </div>
              <div class="cc-inpWarp">
                <input type="text" placeholder="输入名称／拼音首字母">
              </div>
            </div>
            <ul v-for="(classifyNames, index) in classifyList" :key="index">
              <li class="c-c-item clearfix">
                <div class="fl">{{classifyNames.className}}</div>
                <div class="fr">&gt;</div>
              </li>
            </ul>
          </div> -->

        </div>

        <!-- 展示当前用户已选择的分类 -->
        <div class="cate-path clearfix">
          <div class="c-p-title fl">您当前选择的是：</div>
          <div class="c-p-list fl">
            <ol class="clearfix">
              <li>{{this.breakcrumb.join('>')}}</li>
            </ol>
          </div>
        </div>

        <!-- 返回上一步，进行下一步 -->
        <div class="classifySubmit">
          <w-button @on-click="$route.go(-1)">上一步</w-button>
          <w-button @on-click="onNextStep">下一步</w-button>
        </div>
      </div>

    </div>
  </div>
 <!-- 分割线 -->
</template>

<script>
import { apiClassifyMange } from 'js/api'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      threeMenuCode: this.$route.query.three || '',
      classifyList: [], // 分类数据
      breakcrumb: [], // 选中的分类
      gcid: '', // 选中的最后一级分类id
      levels: '' // 选中的分类层级
    }
  },
  computed: {
    ...mapGetters(['token'])
  },
  created() {
    this.fetchClassifyList('', 0)
  },
  methods: {
    /**
     * 获取分类列表
     */
    fetchClassifyList(classifyId, level) {
      // 清空之前的分类数据
      this.classifyList.splice(level, this.classifyList.length - level)
      apiClassifyMange.apiClassifyMenu(this.token, this.threeMenuCode, classifyId).then(resp => {
        if (resp == undefined || resp == 'undefined' || resp.data == 0) {
          return
        }
        this.classifyList.splice(level, 1, resp.data)
      })
    },
    /**
     * 选择当前的分类，展示下级分类
     */
    onNextClassify(classify, list, level) {
      list.forEach((o) => {
        this.$set(o, 'isSelected', false)
      })
      classify.isSelected = !classify.isSelected
      this.breakcrumb[level] = classify.className
      this.fetchClassifyList(classify.id, level + 1)
      this.gcid = classify.id // 选中的分类id
      this.levels = level + 1
    },
    /**
     * 分类查询
     */
    onSearchClassify() {
      // 
    },
    /**
     * 下一步
     */
    onNextStep() {
      const sketchid = this.$route.query.sketchid
      // 编辑
      const crumb = decodeURIComponent(this.breakcrumb.join('>'))
      if (sketchid) {
        this.$router.push(`addInfo?three=${this.$route.query.three}&suid=${this.$route.query.suid}&gcname=${crumb}&ladder=${this.levels}&gcid=${this.gcid}&sketchid=${sketchid}`)
      } else { // 新增
        this.$router.push(`addInfo?three=${this.$route.query.three}&suid=${this.$route.query.suid}&gcname=${crumb}&ladder=${this.levels}&gcid=${this.gcid}`)        
      }
    }
  }
}
</script>

<style  lang="scss">
.goodsClassify {
  .btnWarp button {
    width: 90px;
    height: 46px;
  }
  .classifySubmit{
    width: 310px;
    margin: 0 auto;
    .btn{
      background: #fff;
      color: #222222;
      border: 1px solid #979797;
      border-radius: 4px;
      width: 140px;
      height: 30px;
      font-size: 13px;
      color:rgba(34,34,34,1);
    }
    .btn:nth-child(1){
      margin-right: 24px;
    }
  }
}
</style>

<style lang="scss" scoped>
@import '../../scss/_var.scss';
.btnWarp {
  width: 90px;
  height: 46px;
  background-color: $color-main;
  position: relative;
}
.search-btn {
  position: absolute;
  width: 22px;
  height: 22px;
  margin: auto;
  right: 0;
  top: 0;
  left: 0;
  bottom: 0;
  background: url(../../assets/search-big.png) no-repeat;
}
.goodsClassify {
  margin-top: 20px;
  //  伪类清除浮动
  clearfix:after {
    content: "";
    height: 0;
    display: block;
    clear: both;
  }
  .fl {
    float: left;
  }
  // 右浮动
  .fr {
    float: right;
  }
  .line {
    width: 100%;
    border: 1px solid #d7d7d7;
  }
  // 分类被选中的样式
  .cc-item-select{
    background:rgba(255,229,243,1);
    color: #EF0282!important;
  }
  // 搜索框的样式
  .searchWarp {
    width: 710px;
    height: 46px;
    margin: 0 auto;
    margin-top: 20px;
    margin-bottom: 30px;
    .searchTit {
      width: 80px;
      line-height: 46px;
      font-size: 16px;
      color: #222222;
      font-weight: bold;
    }
    .inpWarp{
      width: 540px;
      height: 46px;
      border: 1px solid #ccc;
      box-sizing:border-box;
      padding: 13px 0 13px 11px;
      // background: #000;
      input{
        height: 20px;
        width: 100%;
      }
    }
  }
  // 分类内容
  .main {
    .lastused-category {
      padding: 16px 0 10px;
      .l-c-title {
        font-size: 14px;
        color: #000;
        font-weight: 700;
      }
      .l-c-item {
        color: #2d8efe;
        cursor: pointer;
      }
    }
    //  类目选择器
    .cate-container{
      height: 553px;
      border: 1px solid #DBDBDB;
      padding-top: 30px;
      padding-left: 23px;
      box-sizing: border-box;
      // 一级二级三级联动公共样式
      .c-c-list{
        width: 356px;
        height: 426px;
        border: 1px solid #DBDBDB;
        margin-right: 30px;
        padding: 10px 13px 0 13px;
        box-sizing: border-box;
        overflow: auto;
        .c-c-item{
          font-size: 14px;
          padding: 8px 8px 8px 15px;
          color: #333333;
          font-weight: bold;
          cursor: pointer;
        }
        // 二级三级搜索框样式
        .cc-cbox-filter{
          height: 36px;
          border: 1px solid #D7D7D7;
          margin-bottom: 10px;
          .icon {
            position: relative;
            height: 36px;
            width: 36px;
            text-align: center;
            line-height: 36px;
          }
          .icon-search{
            position: absolute;
            width: 10px;
            height: 10px;
            top: 0;
            bottom: 0;
            right: 0;
            left: 0;
            margin: auto;
            background: url(../../assets/search-small.png) no-repeat
          }
          .cc-inpWarp{
            widows: auto;
            height: 36px;
            padding: 8px 0 0 36px;
            box-sizing: border-box;
            input{
              width: 100%;
            }
          }
        }
        .actived {
          color: $color-main;
          background-color: #FFE5F3;
        }
      }
      //  展示当前用户已选择的分类
      .cate-path{
        padding-top: 15px;
        .c-p-title{
          font-size: 12px;
          font-weight: bold;
          color: #000;
        }
        .c-p-list{
          li{
            float: left;
            font-size: 12px;
            color: #222222;
          }
        }
      }
    }
  }
}
</style>
